;layui.define(["table", "form","treeGrid"], function(exports) {
    var $ = layui.$
        ,table = layui.table
        ,form = layui.form
        ,treeGrid = layui.treeGrid
        ,tableId = "dict_list_table"
        ,searchFormId="dict-list-search-form";
    treeGrid.render({
        id: tableId,
        elem: "#"+tableId,
        url: '/sso/admin/dict/list',
        method:'post',
        parseData:function (res) {
            return{
                "code":  res.status?res.status:0, //解析接口状态
                "msg": '', //解析提示文本
                "count": res.total, //解析数据长度
                "data": res.records //解析数据列表
            }
        },
        idField:'id',
        treeId:'id',
        treeUpId:'parentId',
        treeShowName:'titleZh',
        isOpenDefault:true,
        iconOpen:false,
        isPage:true,
        heightRemove:[".dHead",200],
        height:'80%',
        cols: [[{
            type: "checkbox",
            fixed: "left",
            width: "5%",
        }, {
            field: "code",
            title: "字典编码",
            width: "15%"
        }, {
            field: "name",
            title: "字典名称",
            width: "10%"
        }, {
            field: "titleZh",
            title: "属性名称(中文)",
            width: "20%"
        }, {
            field: "titleEn",
            title: "属性名称(英文)",
            width: "20%"
        }, {
            field: "value",
            title: "属性值",
            width: "10%"
        },{
            title: "操作",
            width: "20%",
            align: "center",
            fixed: "right",
            toolbar: "#table-useradmin-dict"
        }]],
        page: true,
        limit: 30,
        text: {
            none: '暂无相关数据' //默认：无数据
        },
        done:function (res, curr, count) {
            this.where ={};
            if(res.code == 401){
                layer.msg("没有操作权限,请联系管理员");
            }
            if(res.code == 1001){
                layer.open({
                    title: '提示信息'
                    ,content: '登录失效,请重新登录！',
                    yes:function(index,layero){
                        layer.close(index);
                    },
                    end: function(index, layero){
                        window.location.href="/sso/logout";
                    },
                    time:5000
                });
            }
        }
    });

    treeGrid.on("tool("+tableId+")", function (e) {
        var row = e.data;
        if ("del" === e.event) layer.prompt({
            formType: 0,
            title: "敏感操作，输入口令"
        }, function (value, index) {
            if(value == 'ok'){
                layer.close(index);
                layer.confirm('确定删除行吗？', function (t) {
                    $.get("/sso/admin/dict/remove",{ids:row.id},function(data){
                        if(data.status == 200){
                            e.del(), layer.close(t),layer.msg("删除成功"),table.reload('dict_list_table');
                        }else{
                            layer.close(t),layer.msg("删除失败，请重试");
                        }
                    });
                });
            }else{
                layer.msg('口令错误');
            }
        });
        if ("edit" === e.event) {
            layer.open({
                type: 2,
                title: "编辑数据字典 EDIT",
                content: '/sso/admin/dict/toEditPage?id='+row.id,
                maxmin: !0,
                area: ["480px", "550px"]
            })
        }
        if ("add" === e.event) {
            layer.open({
                type: 2,
                title: "添加下级字典数据 ADD",
                content: '/sso/admin/dict/toAddPage?id='+row.id,
                maxmin: !0,
                area: ["480px", "550px"]
            })
        }
    });

    //事件
    var active = {
        batchDel: function () {
            var checkStatus = table.checkStatus(tableId)
                , checkData = checkStatus.data
            var deList = []; //得到选中的数据
            console.log(checkData);
            if (checkData.length === 0) {
                return layer.msg('请选择数据');
            }
            checkData.forEach(function (row,index) {
                deList.push(row.id);
            });

            layer.prompt({
                formType: 0
                , title: '敏感操作，请验证口令'
            }, function (value, index) {
                if(value == 'ok'){
                    layer.close(index);
                    layer.confirm('确定删除吗？', function (index) {
                        $.get("/sso/admin/dict/remove",{ids:deList.join()},function(data,status){
                            if(data.status == 200){
                                table.reload(tableId);
                                layer.msg("删除成功");
                            }else{
                                layer.msg("删除失败，请重试");
                            }
                        });

                    });
                }else{
                    layer.msg('口令错误');
                }

            });
        }, add: function () {
            layer.open({
                type: 2,
                title: '添加数据字典 ADD',
                content: '/sso/admin/dict/toAddPage?id=',
                maxmin: true,
                area: ['480px', '550px']
            });
        },open:function () {
            var treeData=treeGrid.getDataTreeList(tableId);
            var that= !treeData[0][treeGrid.config.cols.isOpen];
            treeGrid.treeOpenAll(tableId,that);
        },reload: function () {
            var field = $(searchFormId).serializeObject();
            treeGrid.reload(tableId,{
                where:field
            });

        },reset: function () {
            treeGrid.reload(tableId);
        }
    };

    $('.toolbar-button').on('click', function () {
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });

    exports("dict", {});
});
